#!/usr/bin/env python
#-*- coding:utf-8 -*-

import urllib2
from bs4 import BeautifulSoup
from mylog import MyLog
import codecs
from resource import USER_AGENTS, PROXYS
import random


class MusicItem(object):
    top_num = None
    name = None
    singer = None
    duration = None


class GetMusic(object):
    def __init__(self):
        # self.url = 'http://www.kugou.com/yy/html/rank.html'
        self.url = 'http://localhost:8888'
        self.items = []
        self.log = MyLog()

        self.items = self.spider(self.url)
        self.pipelines(self.items)


    def get_response_content(self, url):
        headers = {'User-Agent': random.choice(USER_AGENTS)}
        request = urllib2.Request(url, headers=headers)
        try:
            response = urllib2.urlopen(request)
        except:
            self.log.error('Url: %s Open Error' % url)
            return None
        else:
            self.log.info('Url: %s Open Success' % url)
            return response.read()


    def spider(self, url):
        html_content = self.get_response_content(url)
        soup = BeautifulSoup(html_content, 'lxml')

        musics = soup.find('div', attrs={'id': 'rankWrap'})
        musics = musics.find_all('div')
        musics = musics[1].find('ul')
        musics = musics.find_all('li')

        items = []
        for music in musics:
            self.log.info('Get A Music')
            top_num = music.attrs['data-index']
            self.log.info(top_num)
            song = music.find('a', attrs={'class': 'pc_temp_songname'}).get_text()
            song_list = song.split('-')
            name = song_list[-1]
            singer = '-'.join(song_list[0:-1])
            self.log.info(name)
            self.log.info(singer)
            duration = music.find('span', attrs={'class': 'pc_temp_time'}).get_text()

            item = MusicItem()
            item.top_num = top_num
            item.name = name
            item.singer = singer
            item.duration = duration

            items.append(item)

        return items


    def pipelines(self, items):
        filename = 'music.txt'

        with codecs.open(filename, 'w', 'utf8') as fp:
            for item in items:
                fp.write(item.top_num.strip() + ' ')
                fp.write(item.name.strip() + ' ')
                fp.write(item.singer.strip() + ' ')
                fp.write(item.duration.strip() + '\n')

        self.log.info('Write TO File %s Success' % filename)



if __name__ == '__main__':
    GM = GetMusic()
